const bodyParser = require('body-parser')
const AlipaySdk = require('alipay-sdk').default;
const {
  banners,
  menus,
  lists
} = require('./mock')
// 普通公钥模式
const alipaySdk = new AlipaySdk({
  appId: '2021000119601763',
  // keyType: 'PKCS1', // 默认值。请与生成的密钥格式保持一致，参考平台配置一节
  // 应用私钥
  privateKey: `MIIEowIBAAKCAQEAw8n+AicnWKCsuW0IaBaWBBU07cMuBLvDPG9AXjSOwIcEH9udgbwvLhYL2FNVL5icv9Dd/VxDBfh6ruNqovhatq1lX8psa32+MiuJbfVqysoxnuGboV9aXfl8XDcrrzW42JVkTzfy73PC6iOeccYH9YFzL8jJdQN4Iov9swAGVA7n87TkI/I+NhI/hz3bC7AXYjF9Ozi+/pXRel/YsptfmmHteSuQ9R1N7q1pQceuF5fSPh3eVEKn94OvO1fzmQdR0uBu3c0RFhWD1WaINsKSsSP0vihDUwNJK74XoTrbZVnvOhdpKTCH8sNqM8ST+M3UZ+WO/Ia1Q1imS1dMG6jM+QIDAQABAoIBAHeR7Kaz8ruVpOa5R+EJqiRkLyblhD0vzlQiPVVLE8NQsZwjK1YH72hd3m4GtoeS1GfwzyHkLqp+za3xBTbyJbRawQZ3QS+EY5uQKyYUM1wVj6hQcBNlgHLT4rcOmocub+0Vj2Ux7hN+xxLnHeSt5D5zCD2uihoEPGIczJt88GB0hi+JqZitGWs+X0voQouPusAfW4F/qN+mgcnxGrnQ/5raWTFOlLPw/uxNdsZTdQ4fjfGTpOkK859uDN9rjxcvacWMRS4QFqg7UZuS1DQ0cKm6jG4PPBZqPf+4fyLQjZYyssbdQ5lxrO9bO8Bw1Pqza+nfUkN5S5Xf2B5MM4D1Y4ECgYEA/2QpVl31YFaKqDVuF6pdExGK8QIuqbOZmSBa5g0+YY9v0QEGJl61HzHfooHVGlrXa/V6+dL1MLFDD6SvWyVjfSIXSJ4shNQ2L7yBdmd/ZTTYMLNEfaIedJCvAgxI2UfwstsDO3VDz90ZSK7h72JGLjjnIhUznu6l5NnPqjHF0A0CgYEAx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57BiQqa3Rqjro6/jvc7ByV1fOSxyXIMyuWQIDAQAB',
  // 网关
  gateway: 'https://openapi.alipaydev.com/gateway.do'
});
module.exports = app => {
  app.use(bodyParser.json())
  app.post('/api/pay/zfb', async (req, res) => {
    // 1. 获取支付必要的订单参数
    const {
      orderID,
      price,
      title
    } = req.body;
    // 2. 请求支付宝获取支付链接
    const payUrl = await alipaySdk.pageExec('alipay.trade.wap.pay', {
      // 获取支付链接
      method: 'GET',
      // 参数集合
      bizContent: {
        out_trade_no: orderID,
        total_amount: price,
        subject: title
      },
      // 支付成功回调地址
      returnUrl: 'http://localhost:3000/pay/result'
    })
    res.send({
      code: 200,
      data: payUrl,
      message: 'ok'
    })
  })

  // 
  app.get('/api/pay/query', async (req, res) => {
    // 1. 获取查询参数
    const {
      out_trade_no,
      trade_no
    } = req.query;
    // 2. 向支付宝发送查询请求
    const resp = await alipaySdk.exec('alipay.trade.query', {
      biz_content: {
        out_trade_no,
        trade_no
      }
    })
    // 3. 数据二次处理
    const {
      code,
      tradeStatus
    } = resp
    if (code === '10000') {
      let message = '';
      switch (tradeStatus) {
        case 'WAIT_BUYER_PAY':
          message = '交易创建，等待买家付款';
          break
        case 'TRADE_CLOSED':
          message = '未付款交易超时关闭，或支付完成后全额退款';
          break;
        case 'TRADE_SUCCESS':
          message = '交易支付成功';
          break;
        case 'TRADE_FINISHED':
          message = '交易结束，不可退款';
          break;
        default: message = '未找到该状态'
      }
      res.send({
        code: 200,
        message
      })
    } else if (code === '40004') {
      res.send({
        code: 404,
        message: '交易未找到'
      })
    }
  })


  app.get('/api/banners', (req, res) => {
    res.send({
      code: 200,
      data: banners,
      message: 'ok'
    })
  })
  app.get('/api/menus', (req, res) => {
    res.send({
      code: 200,
      data: menus,
      message: 'ok'
    })
  })
  app.get('/api/lists', (req, res) => {
    const {
      pageSize = 10, pageCode = 1
    } = req.query;


    res.send({
      code: 200,
      data: lists.slice((pageCode - 1) * pageSize, pageCode * pageSize),
      message: 'ok'
    })
  })

  app.get('/api/list/:id', (req, res) => {
    const {
      id
    } = req.params; // get 获取动态路由
    const item = lists.find(item => item.id === id);
    res.send({
      code: 200,
      data: item,
      message: 'ok'
    })
  })
}